<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>$animate</title>
    <script src="http://cdn.bootcss.com/jquery/3.0.0-alpha1/jquery.js"></script>
    <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular-animate.min.js"></script>
    <style type="text/css" media="screen">
        .fade { width: 100px;
            height: 100px;
            background-color: #ff9933;
            transition: 1s linear all;
            -webkit-transition: 1s linear all; }
        .fade.ng-enter { opacity: 0; }
        .fade.ng-enter.ng-enter-active { opacity: 1; }
        .fade.ng-leave { opacity: 1; }
        .fade.ng-leave.ng-leave-active { opacity: 0; }
    </style>
</head>
<body ng-app="Demo" ng-controller="testCtrl as ctrl">
<button my-dir>Fade in/out</button>
<script type="text/javascript">
    (function(){
        var app = angular.module('Demo', ["ngAnimate"])
            .directive("myDir", ["$animate", "$compile", myDir])
            .controller('testCtrl',angular.noop);
        function myDir($animate, $compile) {
            function link(scope, element, attr) {
                var isAppended = false;
                var parent = element.parent();
                var box;
                element.on('click', function () {
                    isAppended = !isAppended;
                    if (isAppended) {
                        box = angular.element('<div class="fade"></div>');
                        scope.$apply($animate.enter(box, parent, element, function () {
                            console.log("Done entering");
                        }));
                    } else {
                        scope.$apply($animate.leave(box, function () {
                            console.log("Done leaving");
                        }));
                    }
                });
            }
            return {
                link: link
            };
        }
    }());
</script>
</body>
</html>